package send;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

public class ServiceDemo {

	
	public static String HttpsClientGet(String address,Map<String,String> head,Map<String,String> params) {
		StringBuilder builder = new StringBuilder();
		try {
			//1、添加请求参数
			if(params!=null){
				List<NameValuePair> param = new ArrayList<NameValuePair>();
				for (Entry<String, String> entry : params.entrySet()) {
					param.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
				}
				UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(param,"UTF-8");
				String str = EntityUtils.toString(encodedFormEntity);
				address = address+"?"+str;
			}
			//2、信任所有证书
			HttpClient client = createSSLClientDefault();
			HttpGet httpGet = new HttpGet(address);
			//3、添加请求头
			if(head!=null){
				for (Entry<String, String> entry : head.entrySet()) {
					httpGet.setHeader(entry.getKey(), entry.getValue());
				}
			}
			//4、发送请求
			HttpResponse response = client.execute(httpGet);
			builder = new StringBuilder();
			//5、返回相应体
			builder.append(new String(getByte(response.getEntity().getContent()),"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return builder.toString();
	}	
	
	public static CloseableHttpClient createSSLClientDefault(){
		try {
		      SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
		                 //信任所有证书
		             public boolean isTrusted(X509Certificate[] chain,
		                                 String authType) throws CertificateException {
		                     return true;
		                 }
		             }).build();
		             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
		             return HttpClients.custom().setSSLSocketFactory(sslsf).build();
		         } catch (KeyManagementException e) {
		             e.printStackTrace();
		         } catch (NoSuchAlgorithmException e) {
		             e.printStackTrace();
		         } catch (KeyStoreException e) {
		             e.printStackTrace();
		         }
		         return  HttpClients.createDefault();
		}	

	public static byte[] getByte(InputStream inputStream) throws Exception{
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		int len = 0;
		byte[] b = new byte[1024];
		while ((len = inputStream.read(b))!=-1) {
			byteArrayOutputStream.write(b, 0, len);
			byteArrayOutputStream.flush();
		}
		inputStream.close();
		byteArrayOutputStream.close();
		return byteArrayOutputStream.toByteArray();
	} 	
	
	public static void main(String[] args) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("cid", "");
		param.put("companyId", "9bfdf49d67ce43b9a2b9ddshengtian1");
		param.put("content", "");
		param.put("email", "");
		param.put("endDate", "2017-10-30 23:59:59");
		param.put("groupId", "");
		param.put("is2Human", "");
		param.put("isQueue", "");
		param.put("lastStaffId", "");
		param.put("operationId", "");
		param.put("page", "1");
		param.put("partnerId", "");
		param.put("questionStatus", "-4");
		param.put("reqType", "");
		param.put("score", "");
		param.put("size", "10");
		param.put("source", "-4");
		param.put("startDate", "2017-09-30 00:00:00");
		param.put("tel", "");
		param.put("uname", "");
		param.put("userDockingId", "");
		param.put("visitorId", "3e3d26d14ac3ad9e5a42775800f6d1ce");
//		String res = HttpUtils.HttpsClientGet("http://kf.stnts.com/data/queryConversation/4", null, param);
//		System.out.println(res);
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date d = f.parse("2017-10-30 13:40:05");
			System.out.println((d.getTime()/1000)%6);
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
	}	
}
